<?php
/**
 * @version     vtaskit pre 1
 * @author      zhiguo.tan@yidatec.com 
 * @date        4/6/2011
 * @copyright   Copyright &copy; 2011,yidatec.com
 */

$abspath = dirname(__FILE__);
require_once '../../../lib/webload.php';
Header("Content-type:text/html;charset=$charset"); 

$todo = _get('todo');
$num = _get('num');
switch ($todo) {
    case ("recent") : 
        $userid = $_VSESS['pass_id'];

        $fd = $myfds = $marks = array();
        $myfds[] = $userid;
        $cachedata = $slave->allcache("SELECT F.fd_id,F.fd_mark,P.pass_icon,P.pass_truename,P.pass_nickname,P.pass_lastlogin,S.sm_sessid,S.sm_activity,S.sm_live FROM {PRE}sns_friend AS F,".DB_PASSPORT.".{PRE}passport AS P LEFT JOIN {PRE}sess_member AS S ON P.pass_id=S.pass_id WHERE P.pass_id=F.fd_id AND F.pass_id='$userid' ORDER BY P.pass_lastlogin DESC, F.sys_time DESC LIMIT $num");
        foreach ($cachedata as $friend) {
            if ($friend['pass_icon'] && is_file($dir['upload'].'icon/'.$friend['pass_icon'])) {
                $icon = $url['upload'].'icon/'.$friend['pass_icon'];
            }else {
                $icon = $url['upload'].'blank/face.gif';
            }
            if ($friend['sm_sessid']) {
                if ($friend['sm_live']=='busy' || $friend['sm_live']=='hide') {
                    $isonline = $friend['sm_live'];
                }else if ((time() - $friend['sm_activity'])/60 > 2) {
                    $isonline = 'idle';
                }else {
                    $isonline = 'free';
                }
            }else {
                $isonline = '0';
            }

            $fd[] =array(
                'fd_id'=>$friend['fd_id'],
                'fd_mark'=>$friend['fd_mark'],
                'pass_lastlogin'=>gmdate('m-j H:i',$friend['pass_lastlogin'] + 3600 * $timeoffset),
                'icon'=>$icon,
                'pass_nickname' => $friend['pass_nickname']?$friend['pass_nickname']:$friend['fd_id'],
                'pass_truename' => $friend['pass_truename']?$friend['pass_truename']:$friend['fd_id'],
                'isonline'=>$isonline,
            );
            $myfds[] = $friend['fd_id'];
            if ($friend['fd_mark']=='1') {
                $marks[] = $friend['fd_id'];
            }
        }

        $tpl->define(array( 
            'index' => 'widgets-recent.tpl',
        )); 
        $tpl->assign(array( 
            'fd'  =>$fd, 
        )); 
        if($preassign && count($preassign)) $tpl->assign($preassign);

        /* tpl print */
        $tpl->print_('index'); 
        break;
    default :
}
?>